'''
Created on 2019年4月6日

@author: bkd
'''
import sys
import optparse
from os import walk, listdir
from os.path import expanduser, dirname, join, splitext, basename
from PyQt5.QtGui import QTextCursor, QIcon
from PyQt5.uic import loadUi
from PyQt5.QtCore import pyqtSlot
from PyQt5.QtWidgets import QWidget, QApplication, QFileDialog, QMessageBox
from PyQt5.uic.driver import Driver
from .fileutil import get_file_realpath
from .kdui2py_ui import Ui_Form


class kdui2py(QWidget, Ui_Form):

    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.setWindowIcon(QIcon(get_file_realpath("kdui2py.jpg")))
        print(get_file_realpath("kdui2py.jpg"))
        self.last_dir = None
        self.last_file = None
        self.last_action = None

    # 转单个目录
    @pyqtSlot()
    def on_pb_convert_single_file_clicked(self):
        self.last_file, _ = QFileDialog.getOpenFileName(
            self, '转换单个文件', self.get_last_dir(), '*.ui', '')
        self.last_dir = dirname(self.last_file)
        output_file = self.convert_single_file(self.last_file)
        self.last_action = "single_file"

    # 转单个目录
    @pyqtSlot()
    def on_pb_single_dir_clicked(self):
        self.last_dir = QFileDialog.getExistingDirectory(
            self, '转换目录', self.get_last_dir())
        self.conver_single_dir()
        self.last_action = "single_dir"

    # 转单个目录
    def conver_single_dir(self):
        if not self.last_dir:
            return
        self.tb_result.append("目录：{}".format(self.last_dir))
        files = listdir(self.last_dir)
        for file in files:
            if file.endswith(".ui"):
                absolute_path = join(self.last_dir, file)
                output_file = self.convert_single_file(absolute_path)

    # 转换目录及其子目录
    @pyqtSlot()
    def on_pb_dir_and_subdir_clicked(self):
        self.last_dir = QFileDialog.getExistingDirectory(
            self, '转换目录及其子目录', self.get_last_dir())
        self.conver_dir_and_subdir()
        self.last_action = "dirAndSubDir"

    # 转换目录及其子目录
    def conver_dir_and_subdir(self):
        if not self.last_dir:
            return
        self.tb_result.append("目录：{}".format(self.last_dir))
        # for dirpath,dirnames,filenames in walk(seleted_dir):
        for dirpath, _, filenames in walk(self.last_dir):
            for file in filenames:
                if file.endswith(".ui"):
                    absolute_path = join(dirpath, file)
                    output_file = self.convert_single_file(absolute_path)

    # 预览UI文件
    @pyqtSlot()
    def on_pb_preview_clicked(self):
        self.last_file, _ = QFileDialog.getOpenFileName(
            self, '预览UI文件', expanduser("~"), '*.ui', '')
        if not self.last_file:
            return
        try:
            self.preview_widget = loadUi(self.last_file)
            self.preview_widget.show()
            self.last_action = "preview"
        except Exception as e:
            QMessageBox.warning(self, "预览文件失败", str(e), QMessageBox.Ok)

    # 获取上一次打开的目录
    def get_last_dir(self):
        if self.last_dir:
            return self.last_dir
        else:
            return expanduser("~")

# 转换单个文件
    def convert_single_file(self, filename, preview=False):
        if filename:
            base_name = basename(filename)
            output_file = join(dirname(filename), splitext(
                base_name)[0] + self.le_suffix.text() + ".py")
            # complete sample : opts: {'preview': False, 'output': '/tmp/r.oo', 'execute': False, 'debug': False, 'indent': 4, 'import_from': None, 'from_imports': False, 'resource_suffix': '_ui'}
            opts = optparse.Values()
            opts.ensure_value("preview", preview)
            opts.ensure_value("execute", False)
            opts.ensure_value("debug", False)
            opts.ensure_value("indent", 4)
            opts.ensure_value("from_imports", False)
            opts.ensure_value("import_from", None)
            opts.ensure_value("resource_suffix", "_rc")
            opts.ensure_value("output", output_file)
            args = filename
            driver = Driver(opts, args)
            try:
                driver.invoke()
                self.tb_result.append(
                    "转换成功，{} -> {}".format(basename(filename), basename(output_file)))
                # self.tb_result.append("\n")
                self.tb_result.moveCursor(QTextCursor.End)
                return output_file
            except IOError as e:
                QMessageBox.warning(self, "转换异常", str(e), QMessageBox.Ok)

    # 重复上次操作
    @pyqtSlot()
    def on_pb_redo_clicked(self):
        if self.last_action == "single_file":
            self.convert_single_file(self.last_file)
        elif self.last_action == "single_dir":
            self.conver_single_dir()
        elif self.last_action == "dirAndSubDir":
            self.conver_dir_and_subdir()
        elif self.last_action == "preview":
            if not self.last_file:
                return
            self.preview_widget = loadUi(self.last_file)
            self.preview_widget.show()


def main():
    app = QApplication(sys.argv)
    win = kdui2py()
    win.show()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()
